package com.oppo.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oppo.bean.UserBean;
import com.oppo.service.impl.RegistService;
import com.oppo.tools.MyTools;

@WebServlet("/RegistControl")
public class RegistControl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		UserBean one = new UserBean();
		String user_id = request.getSession().getAttribute("user_id_copy").toString();
		one.setUser_id(new Long(user_id));
		request.getSession().removeAttribute("user_id_copy");
		int i = MyTools.getNewInstance(RegistService.class).Regist(one);
		if(i > 0) {
			MyTools.logRootLogger.info(one.getUser_id() + "注册成功！");
			request.getSession().setAttribute("user_id", one.getUser_id());// 缓存登录成功的用户信息
			//WEB_INF目录是站点安全目录 无法直接使用URL直接访问  应用转发形式访问
			request.getRequestDispatcher("/WEB-INF/user/UserAddInfo.jsp").forward(request, response);
		}else {
			MyTools.logRootLogger.info(one.getUser_id() + "注册失败！");
			response.sendRedirect("Register.html");
		} 
		MyTools.logRootLogger.info("时间：" + MyTools.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
	}

}
